/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Adapter;

import be.ac.ulg.montefiore.run.jahmm.*;
import Domain.*;
import java.util.ArrayList;
import java.util.List;
/**
 *
 * @author Allen
 * @author Ryan
 */
public class DomainAdapter {

    public static Hmm<ObservationDiscrete<Output>>
            cloneHmm(Hmm<ObservationDiscrete<Output>> hmm){
        double T[][] = new double[3][3];
        double O[][] = new double[3][3];
        DomainAdapter.toMatrix(hmm, T, O);
        return DomainAdapter.toHmm(T, O);

    }
    public static Hmm<ObservationDiscrete<Output>> toHmm(double T[][],
                                                            double O[][]) {
        Hmm<ObservationDiscrete<Output>> hmm =
			new Hmm<ObservationDiscrete<Output>>( 3,
				new OpdfDiscreteFactory<Output>(Output.class));
        //setting inital state probability
	hmm.setPi(0, 1.0);
	hmm.setPi(1, 0.0);
        hmm.setPi(2, 0.0);

        //setting observation probability
        for ( int i=0; i<3; i++)
            hmm.setOpdf(i, new OpdfDiscrete<Output>(Output.class,O[i]));

        //setting transition probability
        for (int i=0; i < 3; i++)
            for (int j=0; j < 3; j++)
                hmm.setAij(i, j, T[i][j]);

        return hmm;
    }

    public static Output[] toOutput() {
        return null;
    }

    public static List<ObservationDiscrete<Output>> toObservation(
                                                        Output[] outputlist ) {
        List<ObservationDiscrete<Output>> loc_list =
                                new ArrayList<ObservationDiscrete<Output>>();
        for ( int i=0; i < outputlist.length; i++)
            loc_list.add( new ObservationDiscrete(outputlist[i]));

        return loc_list;
    }
    public static void toMatrix
            (Hmm<ObservationDiscrete<Output>> hmm, double[][] T, double[][] O ){

        Output loc_c = Output.c;
        Output loc_e = Output.e;
        Output loc_r = Output.r;

        for ( int i=0; i<3; i++)
            for ( int j=0; j<3; j++)
                T[i][j] = hmm.getAij(i, j);

        for ( int i=0; i<3; i++) {
            O[i][0] = hmm.getOpdf(i).probability
                        (new ObservationDiscrete<Output>(loc_c));
            O[i][1] = hmm.getOpdf(i).probability
                        (new ObservationDiscrete<Output>(loc_e));
            O[i][2] = hmm.getOpdf(i).probability
                        (new ObservationDiscrete<Output>(loc_r));
        }
    }
    public State[] itoState( int [] array) {
        State loc_state = State.C;

        State [] result = new State[array.length];
        for (int i = 0; i < array.length; i++)
            result[i] =loc_state.toState( array[i] );  
        return result;
    }
}
